home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d123456 / CHEMPLOT.ZIP / TPlot / BCB Demo / Normal1.cpp < prev    next >
C/C++ Source or Header  |  2001-07-25  |  13KB  |  446 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3.  
  4. #include <math.h>
  5.  
  6. #pragma hdrstop
  7.  
  8. #include "Normal1.h"
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma link "Plot"
  12. #pragma link "PlotMenu"
  13. #pragma link "NEdit"
  14. #pragma link "Nedit"
  15. #pragma link "Plotimagelist"
  16. #pragma link "Plotmenu"
  17. #pragma link "Plottoolbar"
  18. #pragma resource "*.dfm"
  19. TMainForm *MainForm;
  20. //---------------------------------------------------------------------------
  21. __fastcall TMainForm::TMainForm(TComponent* Owner)
  22.     : TForm(Owner)
  23. {
  24. }
  25. //---------------------------------------------------------------------------
  26. void __fastcall TMainForm::FormCreate(TObject *Sender)
  27. {
  28.   Height = 516;
  29.   Width = 712;
  30.  
  31.   MyPlot = new TPlot(this);
  32.   MyPlot->Parent = this;
  33.   MyPlot->Align = alClient;
  34.   MyPlot->PlotType = ptXY;
  35.   MyPlot->NoSeries = 2;
  36.   MyPlot->MakeDummyData(20);
  37.  
  38.   MyPlotImageList = new TPlotImageList(this);
  39. #ifdef COMPILER4_UP
  40.   MyPlot->Images = MyPlotImageList;
  41. #endif
  42.   MyPlotMenu = new TPlotMenu(this);
  43. #ifdef COMPILER4_UP
  44.   MyPlotMenu->Images = MyPlotImageList;
  45. #endif
  46.   MyPlotMenu->Plot = MyPlot;
  47.   //MyPlotMenu->SetUpOnClicks;
  48.  
  49.   MyPlotToolBar = new TPlotToolBar(this);
  50.   MyPlotToolBar->Parent = this;
  51.   MyPlotToolBar->Images = MyPlotImageList;
  52.   MyPlotToolBar->Plot = MyPlot;   
  53.  
  54.   StringGrid1->Cells[0][0] = "Test:";
  55.   StringGrid1->Cells[0][1] = "Score:";
  56.   StringGrid1->ColWidths[0] = 60;
  57. }
  58. //---------------------------------------------------------------------------
  59.  
  60. void __fastcall TMainForm::ExitMenuItemClick(TObject *Sender)
  61. {
  62.   Close();
  63. }
  64. //---------------------------------------------------------------------------
  65.  
  66. void __fastcall TMainForm::GoBitBtnClick(TObject *Sender)
  67. {
  68.   Single X, Y;
  69.   Single Mean;
  70.   Single StdDev;
  71.   Single Min;
  72.   Single Max;
  73.   Single StepSize;
  74.   Integer TheSeries;
  75.   Single TheScore;
  76.   Integer i;
  77.   PSeries pMySeries;
  78.  
  79.   MyPlot->SeriesList->ClearSeries();
  80.   MyPlot->AddSeries(-1);
  81.  
  82.   Mean = MeanNEdit->AsReal;
  83.   StdDev = StdDevNEdit->AsReal;
  84.   Min = MinNEdit->AsReal;
  85.   Max = MaxNEdit->AsReal;
  86.   StepSize = StepSizeNEdit->AsReal;
  87.  
  88. //Set the axes:
  89.   MyPlot->XAxis->Max = Max;
  90.   MyPlot->XAxis->Min = Min;
  91.   MyPlot->YAxis->Min = 0;
  92.   MyPlot->XAxis->Intercept = 0;
  93.  
  94.   X = Min;
  95.   pMySeries = (PSeries) MyPlot->SeriesList->Items[0];
  96.   do
  97.   {
  98.     Y = (X-Mean)/(2*StdDev);
  99.     Y = Y * Y;
  100.     Y = exp(-Y) / sqrt(2 * PI * StdDev);
  101. //Don't fire any events, and don't adjust axes:
  102.     pMySeries->AddPoint(X, Y, FALSE, FALSE);
  103.     X = X + StepSize;
  104.   } while (X <= Max);
  105.   pMySeries->Visible = TRUE;
  106.  
  107.   MyPlot->YAxis->Min = MyPlot->Series[0]->YMin;
  108.   MyPlot->YAxis->Max = pMySeries->YMax;
  109.  
  110.   for (i = 1; i < StringGrid1->ColCount; i++)
  111.   {
  112.     if (StringGrid1->Cells[i][0].Length() > 0)
  113.     {
  114.       if (StringGrid1->Cells[i][1].Length() > 0)
  115.       {
  116.         try
  117.         {
  118.           TheScore = StrToFloat(StringGrid1->Cells[i][1]);
  119.           TheSeries = MyPlot->AddSeries(-1);
  120.           pMySeries = (PSeries) MyPlot->SeriesList->Items[TheSeries];
  121.           pMySeries->Name = StringGrid1->Cells[i][0];
  122.           pMySeries->AddPoint(TheScore, MyPlot->YAxis->Min, TRUE, TRUE);
  123.           pMySeries->AddPoint(TheScore, MyPlot->YAxis->Max, TRUE, TRUE);
  124.           pMySeries->Visible = TRUE;
  125.           pMySeries->Symbol = (TSymbol) fmod(i, 1+syDownTriangle);
  126.         }
  127.         __finally 
  128.         {}
  129.       }
  130.     }
  131.   }
  132.  
  133. }
  134. //---------------------------------------------------------------------------
  135.  
  136. void __fastcall TMainForm::PlotTypeBitBtnClick(TObject *Sender)
  137. {
  138.   Integer ThePlotType;
  139.  
  140.   ThePlotType = MyPlot->PlotType;
  141.   ThePlotType++;
  142.   if (ThePlotType > pt3DColumn) ThePlotType = 0;
  143.  
  144.   MyPlot->SeriesList->ClearSeries();
  145.  
  146.   //Initialise properties that can change:
  147.   MyPlot->YAxis->LimitsVisible = FALSE;
  148.   MyPlot->ContourInterval = 1;
  149.   MyPlot->ContourDetail = cdLow;
  150.   MyPlot->ContourWireFrame = FALSE;
  151.   MyPlot->YAxis->TickDirection = orLeft;
  152.   MyPlot->YAxis->Title->Orientation = orLeft;
  153.   MyPlot->YAxis->Min = 0;
  154.  
  155.   MyPlot->PlotType = (TPlotType) ThePlotType;
  156.  
  157.   MyPlot->Title->Caption = "TPlot - " + MyPlot->GetPlotTypeAsString();
  158.  
  159.   switch (MyPlot->PlotType)
  160.   {
  161.     case ptXY:
  162.       {
  163.         MyPlot->MakeDummyData(100);
  164.         MyPlot->Border->Left = 70;
  165.         MyPlot->Border->BottomGap = 80;
  166.         FormActivate(this);
  167.         break;
  168.       };
  169.     case ptError:
  170.       {
  171.         MyPlot->MakeDummyData(20);
  172.         MyPlot->Series[0]->Symbol = syCircle;
  173.         MyPlot->SetInstructionText("This is boring !");
  174.         break;
  175.       };
  176.     case ptMultiple:
  177.       {
  178.         MyPlot->NoSeries = 4;
  179.         MyPlot->Multiplicity = 4;
  180.         MyPlot->MakeDummyData(20);
  181.         MyPlot->Series[0]->Pen->Width = 0;
  182.         MyPlot->Series[1]->Pen->Width = 0;
  183.         MyPlot->Series[2]->Pen->Width = 0;
  184.         MyPlot->Series[3]->Pen->Width = 0;
  185.         MyPlot->Series[0]->Name = "High";
  186.         MyPlot->Series[1]->Name = "Low";
  187.         MyPlot->Series[2]->Name = "Open";
  188.         MyPlot->Series[3]->Name = "Close";
  189.         MyPlot->Series[0]->Symbol = syLeftDash;
  190.         MyPlot->Series[1]->Symbol = syRightDash;
  191.         MyPlot->MultiJoin = "2,3";
  192.         MyPlot->SetInstructionText("Please wait while I add some Limits ...");
  193.         Wait(2000, FALSE);
  194.         MyPlot->YAxis->LimitLower = 2;
  195.         MyPlot->YAxis->LimitUpper = 9;
  196.         MyPlot->YAxis->LimitsVisible = TRUE;
  197.         MyPlot->Series[3]->ShadeLimits = TRUE;
  198.         MyPlot->SetInstructionText("Done !");
  199.         break;
  200.       };
  201.     case ptBubble:
  202.       {
  203.         MyPlot->MakeDummyData(20);
  204.         MyPlot->SetInstructionText("This is boring !");
  205.         break;
  206.       };
  207.     case ptColumn:
  208.     case ptStack:
  209.     case ptNormStack:
  210.       {
  211.         MyPlot->Grid = gtNone;
  212.         MyPlot->MakeDummyData(10);
  213.         MyPlot->SetInstructionText("This is boring !");
  214.         break;
  215.       };
  216.     case ptPie:
  217.       {
  218.         MyPlot->MakeDummyData(10);
  219.         TheXStringData = new TStringList;
  220.         TheXStringData->Add("Alpha");
  221.         TheXStringData->Add("Bravo");
  222.         TheXStringData->Add("Charlie");
  223.         TheXStringData->Add("Delta");
  224.         TheXStringData->Add("Echo");
  225.         TheXStringData->Add("Foxtrot");
  226.         TheXStringData->Add("Golf");
  227.         TheXStringData->Add("Hotel");
  228.         TheXStringData->Add("India");
  229.         TheXStringData->Add("Juliet");
  230.         TheXStringData->Add("Kilo");
  231.         MyPlot->Series[0]->XStringData = TheXStringData;
  232.         MyPlot->Series[1]->XStringData = TheXStringData;
  233.         delete TheXStringData;
  234.         MyPlot->SetInstructionText("This is very boring !");
  235.         break;
  236.       };
  237.     case ptPolar:
  238.       {
  239.         MyPlot->XAxis->Min = -10;
  240.         MyPlot->XAxis->Max = 10;
  241.         MyPlot->YAxis->Min = -10;
  242.         MyPlot->YAxis->Max = 10;
  243.         MyPlot->MakeDummyData(20);
  244.         MyPlot->XAxis->Intercept = 0;
  245.         MyPlot->YAxis->Intercept = 0;
  246.         MyPlot->SetInstructionText("This is extremely boring !");
  247.         break;
  248.       };
  249.     case ptLineContour:
  250.     case ptContour:
  251.     case pt3DContour:
  252.     case pt3DWire:
  253.     case pt3DColumn:
  254.       {
  255.         if (MyPlot->PlotType > ptContour)
  256.         {
  257.           MyPlot->Border->Left = 120;
  258.           MyPlot->Border->BottomGap = 130;
  259.         };
  260.         MyPlot->XAxis->Min = 0;
  261.         MyPlot->XAxis->Max = 10;
  262.         MyPlot->YAxis->Min = 0;
  263.         MyPlot->YAxis->Max = 10;
  264.         MyPlot->NoSeries = 10;
  265.         MyPlot->MakeDummyData(20);
  266.       };
  267.   };
  268.  
  269.   switch (MyPlot->PlotType)
  270.   {
  271.     case ptLineContour:
  272.       {
  273.         MyPlot->SetInstructionText("Please wait while I increase the ContourDetail ...");
  274.         Wait(2000, FALSE);
  275.         MyPlot->ContourDetail = cdMedium;
  276.         MyPlot->ContourInterval = MyPlot->ContourInterval / 2;
  277.         Wait(2000, FALSE);
  278.         MyPlot->ContourDetail = cdHigh;
  279.         MyPlot->SetInstructionText("Done !");
  280.         break;
  281.       };
  282.     case ptContour:
  283.       {
  284.         MyPlot->SetInstructionText("Please wait while I increase the ContourDetail ...");
  285.         Wait(2000, FALSE);
  286.         MyPlot->ContourDetail = cdMedium;
  287.         Wait(2000, FALSE);
  288.         MyPlot->ContourDetail = cdHigh;
  289.         MyPlot->SetInstructionText("Done !");
  290.         break;
  291.       };
  292.     case pt3DContour:
  293.       {
  294.         MyPlot->SetInstructionText("Please wait while I increase the ContourDetail ...");
  295.         Wait(2000, FALSE);
  296.         MyPlot->ContourDetail = cdMedium;
  297.         Wait(2000, FALSE);
  298.         MyPlot->ContourDetail = cdHigh;
  299.         Wait(2000, FALSE);
  300.         MyPlot->SetInstructionText("... add a wireframe ...");
  301.         Wait(2000, FALSE);
  302.         MyPlot->ContourWireFrame = TRUE;
  303.         MyPlot->SetInstructionText("... add some walls and grids ...");
  304.         Wait(2000, FALSE);
  305.         MyPlot->Grid = gtBoth;
  306.         MyPlot->SetInstructionText("... move the axes to render them more visible ...");
  307.         Wait(2000, FALSE);
  308.         MyPlot->XAxis->Intercept = MyPlot->YAxis->Max;
  309.         Wait(2000, FALSE);
  310.         MyPlot->YAxis->Intercept = MyPlot->XAxis->Max;
  311.         MyPlot->SetInstructionText("... re-arrange the Y Axis Title and Labels");
  312.         Wait(2000, FALSE);
  313.         MyPlot->YAxis->TickDirection = orRight;
  314.         MyPlot->YAxis->Title->Orientation = orRight;
  315.         MyPlot->SetInstructionText("... move the Z Axis ...");
  316.         Wait(2000, FALSE);
  317.         MyPlot->ZAxis->ZInterceptY = MyPlot->YAxis->Max;
  318.         MyPlot->SetInstructionText("Done !");
  319.         MyPlot->Refresh();
  320.         break;
  321.       };
  322.     case pt3DWire:
  323.     case pt3DColumn:
  324.       {
  325.         MyPlot->SetInstructionText("Please wait while I move the axes to render them more visible ...");
  326.         Wait(2000, FALSE);
  327.         MyPlot->XAxis->Intercept = MyPlot->YAxis->Max;
  328.         MyPlot->YAxis->Intercept = MyPlot->XAxis->Max;
  329.         MyPlot->YAxis->TickDirection = orRight;
  330.         MyPlot->YAxis->Title->Orientation = orRight;
  331.         MyPlot->SetInstructionText("Done !");
  332.       };
  333.   };
  334. }
  335. //---------------------------------------------------------------------------
  336.  
  337. void __fastcall TMainForm::BitBtn3Click(TObject *Sender)
  338. {
  339.   MyPlot->MakeDummyData(100);
  340. }
  341. //---------------------------------------------------------------------------
  342.  
  343. void __fastcall TMainForm::TraceBitBtnClick(TObject *Sender)
  344. {
  345.   MyPlot->Trace();    
  346. }
  347. //---------------------------------------------------------------------------
  348.  
  349. void __fastcall TMainForm::ClearAllBitBtnClick(TObject *Sender)
  350. {
  351.   Integer i;
  352.  
  353.   MyPlot->SeriesList->ClearSeries();
  354.   for (i = 1; i < StringGrid1->ColCount; i++)
  355.   {
  356.     StringGrid1->Cells[i][0] = "";
  357.     StringGrid1->Cells[i][1] = "";
  358.   }
  359. }
  360. //---------------------------------------------------------------------------
  361.  
  362. void __fastcall TMainForm::GoCrazyBitBtnClick(TObject *Sender)
  363. {
  364.   if (NULL != MyPlot->OnAfterPaint)
  365.   {
  366.     GoCrazyBitBtn->Caption = "Go Crazy";
  367.     TraceBitBtn->Enabled = TRUE;
  368.     MyPlot->OnAfterPaint = NULL;
  369.   }
  370.   else
  371.   {
  372.     Revolutions = 0;
  373.     StartTime = Now();
  374.     StartWidth = Width;
  375.     StartHeight = Height;
  376.     Angle = 0;
  377.     AngleInc = 4 * PI / 180;
  378.     GoCrazyBitBtn->Caption = "Enough !";
  379.     TraceBitBtn->Enabled = FALSE;
  380.     MyPlot->OnAfterPaint = Plot1AfterPaint;
  381.     MyPlot->Invalidate();
  382.   };
  383. }
  384. //---------------------------------------------------------------------------
  385.  
  386. void __fastcall TMainForm::CrazyTimerTimer(TObject *Sender)
  387. {
  388.   float fpm;
  389.  
  390.   if (MyPlot->PlotType >= pt3DContour)
  391.   {
  392.     MyPlot->ZAngle = MyPlot->ZAngle + 1;
  393.   }
  394.   else
  395.   {
  396.     Width = StartWidth + RADIUS * sin(Angle);
  397.     Height = StartHeight + RADIUS * cos(Angle);
  398.     Angle = Angle + AngleInc;
  399.   };
  400.   Revolutions++;
  401.   ElapsedTime = Now() - StartTime;
  402.   fpm = Revolutions / ((24 * 3600)*ElapsedTime);
  403.  
  404.   TVarRec v[] = { Revolutions, fpm };
  405.   StatusBar1->SimpleText = Format(
  406.     "%d frames, %8.2f frames per second",
  407.     v, ARRAYSIZE(v) - 1);
  408. }
  409. //---------------------------------------------------------------------------
  410.  
  411. void __fastcall TMainForm::MyPlotFileOpen(TObject *Sender, AnsiString TheFile)
  412. {
  413.   AnsiString TheTitle;
  414.  
  415.   TheTitle = ExtractFileName(Application->ExeName);
  416.   //TheTitle = Copy(TheTitle, 1, Length(TheTitle)-4);
  417.   TheTitle.SetLength(TheTitle.Length()-4);
  418.   TheTitle = TheTitle + " - " + ExtractFileName(TheFile);
  419.   Application->Title = TheTitle;
  420.   MainForm->Caption = TheTitle;
  421. }
  422. //---------------------------------------------------------------------------
  423.  
  424. void __fastcall TMainForm::FormActivate(TObject *Sender)
  425. {
  426.   if (ptXY == MyPlot->PlotType)
  427.   {
  428.     MyPlot->SetInstructionText("Please wait while I add some symbols ...");
  429.     Wait(2000, TRUE);
  430.     MyPlot->Series[0]->Symbol = sySquare;
  431.     Wait(2000, TRUE);
  432.     MyPlot->Series[1]->Symbol = syCircle;
  433.     MyPlot->SetInstructionText("Done !");
  434.   };
  435. }
  436. //---------------------------------------------------------------------------
  437.  
  438. void __fastcall TMainForm::Plot1AfterPaint(TObject *Sender,
  439.       TCanvas *ACanvas)
  440. {
  441.   Application->ProcessMessages();
  442.   CrazyTimerTimer(Sender);
  443. }
  444. //---------------------------------------------------------------------------
  445.  
  446.